<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends Controller
{
    public function index()
    {
        $id=session('id');
        $adminmodel=M('admin');
        $admin=$adminmodel->where(array('id'=>$id))->select();
        $this->admin=$admin;
        $newsmodel=M('news');
        $news=$newsmodel->order('id ')->join('users on users.uid=news.n_uid')->select();

        $this->news=$news;
        for ($i = 0; $i < count($news); $i++){
            $news[$i]["code"] = $i + 1;

        if($news[$i]["tid"]== 1){
          $type[$i]='体育';
       }
       elseif($news[$i]["tid"]== 2)
       {
           $type[$i]='时政';

       }
        elseif($news[$i]["tid"]== 3)
        {
            $type[$i]='娱乐';

        }
        elseif($news[$i]["tid"]== 4)
        {
            $type[$i]='科技';

        }

        elseif($news[$i]["tid"]== 5)
        {
            $type[$i]='军事';

        }
        elseif($news[$i]["tid"]== 6)
        {
            $type[$i]='游戏';

        }
        elseif($news[$i]["tid"]== 7)
        {
            $type[$i]='视频';

        }
            $news[$i]["tid"]=$type[$i];
        }

        if(!$id)
        {$this->error('登录已过期!',U("index.php/Admin/Index/index"));}
        elseif($news){
            $this->assign('list', $news);
            $this->display();
        }
        elseif ($admin){
            $this->assign('admin', $admin);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Admin/news/index"));}

    }


    public function del(){
        $id = $_GET['id'];
        $news = M("news");
        $news->id = $id;
        $news->delete();
        if ($news) {
            $this->success('删除新闻成功');
        } else {
            $this->error('删除新闻失败!');

        }

    }



    public function insert(){
        $data['title'] = trim(I('post.title'));
        $data['tid'] = trim(I('post.tid'));
        $data['content'] = trim(I('post.content'));
        $data['satus'] = "1";
        $data['adduser'] = trim(I('post.adduser'));
        $data['top'] = "1";
        $data['publishment'] = date('y-m-d', time());
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/images/pic/'; // 设置附件上传根目录
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $data['n_pic']=$info['savepath'].$info['savename'];
        $result=M('news')->where(array('title'=>$data['title']))->find();
        if($result)
            $this->error('标题重复！', U("News/addnews"));
        else{
            M('news')->add($data);
            //var_dump($data);
            $url=U('News/index');
            header("Location: $url");
            exit(0);
        }


    }


    public function detail(){
        $id = $_GET['id'];
        $newsmodel=M('news');
        $news=$newsmodel->where(array("id" => $id))->join('users on users.uid=news.n_uid')->select();
        for ($i = 0; $i < count($news); $i++){

            if($news[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($news[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($news[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($news[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($news[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($news[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($news[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $news[$i]["tid"]=$type[$i];
        }



        if($news){
            $this->assign('list', $news);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Admin/News/index"));}

    }

public function update(){
    $id = $_GET['id'];
    $newsmodel=M('news');
    $news=$newsmodel->where(array("id" => $id))->join('users on users.uid=news.n_uid')->select();
    for ($i = 0; $i < count($news); $i++){
        if($news[$i]["tid"]== 1){
            $type[$i]='体育';
        }
        elseif($news[$i]["tid"]== 2)
        {
            $type[$i]='时政';

        }
        elseif($news[$i]["tid"]== 3)
        {
            $type[$i]='娱乐';

        }
        elseif($news[$i]["tid"]== 4)
        {
            $type[$i]='科技';

        }

        elseif($news[$i]["tid"]== 5)
        {
            $type[$i]='军事';

        }
        elseif($news[$i]["tid"]== 6)
        {
            $type[$i]='游戏';

        }
        elseif($news[$i]["tid"]== 7)
        {
            $type[$i]='视频';

        }
        $news[$i]["tid"]=$type[$i];
    }



    if($news){
        $this->assign('list', $news);
        $this->display();
    }
    else{ $this->error('获取失败!', U("Admin/News/index"));}
}

public function reset(){
    $data['tid'] = trim(I('post.tid'));
    $x=$data['tid'];
    $data['title'] = trim(I('post.title'));
    $data['content'] = trim(I('post.content'));
    $u = trim(I('post.adduser'));
    $uid=M('users')->where(array("username"=>$u))->select();
    $data['n_uid'] =$uid[0]["uid"];
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize = 3145728 ;// 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath = './Public/images/pic/'; // 设置附件上传根目录
    $upload->autoSub = false;
    $info = $upload->uploadOne($_FILES['pic']);
    if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
    }
    $data['n_pic']=$info['savepath'].$info['savename'];
    $id['id']=trim(I('post.id'));
    $a=M("news");
    if($x==null||$x==0){
        $this->error('请选择新闻类型再修改！', U("News/index"));
    }

    else{
        $a->where($id)->save($data);
            $this->success('修改成功',U("News/index"));
    }
}

    
    public function search(){
        $text = trim(I('post.text'));
        $newsmodel=M('news');
        $a['title']=array('like',"%{$text}%");
        $select=$newsmodel->where($a)->select();
        for ($i = 0; $i < count($select); $i++){
            $select[$i]["code"] = $i + 1;
            if($select[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($select[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($select[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($select[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($select[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($select[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($select[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $select[$i]["tid"]=$type[$i];
        }
        if($select)
        {
            $this->assign('list', $select);
            $this->display();
        }
        else{ $this->error('未查到相关结果,请重新查询',U("News/index"));}
    }


       public function fabulous(){
       $a=M('fabulous')->join('news on news.id=fabulous.fs_id')->join('users on users.uid=fabulous.fs_uid')->select();
        for ($i = 0; $i < count($a); $i++){
            $a[$i]["code"] = $i + 1;
        }
        if($a){
          $this->assign('list', $a);
          $this->display();
              }
             else{ $this->error('未查到相关结果,请重新查询',U("News/index"));}
             }



    public function fssearch(){
        $title = trim(I('post.title'));
        $a['title']=array('like',"%{$title}%");
        $fsselect=M('news')->where($a)->join('fabulous on fabulous.fs_id=news.id')->join('users on users.uid=fabulous.fs_uid')->select();
        for ($i = 0; $i < count($fsselect); $i++){
            $fsselect[$i]["code"] = $i + 1;
        }
        if($fsselect)
        {
            $this->assign('list', $fsselect);
            $this->display();
        }
        else{ $this->error('未查到相关结果,请重新查询',U("News/index"));}
    }


    public function fsdel(){
        $fsid = $_GET['fsid'];
        $fabulous = M("fabulous");
        $fabulous->fsid = $fsid;
        $fabulous->delete();
        if ($fabulous) {
            $this->success('删除该用户点赞成功');
        } else {
            $this->error('删除该用户点赞失败!');

        }
    }


}